﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace syc.Sale
{
    public partial class FuLiao_GuanCai_DaoHuo_Search : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String sql = @"SELECT     dbo.管材到货登记.车号,dbo.原料供货单位.名称, dbo.材料.品名, dbo.管材到货明细.炉号, dbo.管材到货明细.批次, dbo.管材到货明细.重量,dbo.管材到货明细.磅重,dbo.管材到货明细.支数, dbo.管材到货明细.区域, 
                      dbo.管材到货登记.到货日期
                      FROM         dbo.管材到货登记 INNER JOIN
                      dbo.管材到货明细 ON dbo.管材到货登记.ID = dbo.管材到货明细.管材到货登记ID INNER JOIN
                      dbo.原料供货单位 ON dbo.管材到货登记.原料供货单位ID = dbo.原料供货单位.ID INNER JOIN
                      dbo.材料 ON dbo.管材到货明细.管材ID = dbo.材料.ID where 管材类别=1 and dbo.材料.品名='成品套管接箍'";
            this.Message.Text = "·准备就绪";
            if (!this.IsPostBack)
            {
                this.SqlDataSource1.SelectCommand = sql;
                this.ViewState["SelectCommand"] = sql;
            }
            this.SqlDataSource1.SelectCommand = (string)this.ViewState["SelectCommand"];
        }
        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }
        }

        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }

        protected void Button_Query_Click(object sender, EventArgs e)
        {
            string temp = this.CheHao.Text.Trim();
            string temp1 = this.LuHao.Text.Trim();
            string Sql = "";
            if (temp == "" && temp1 == "")
            {
                Sql = "where [管材类别]=1 and dbo.材料.品名='成品套管接箍'";
            }
            else if (temp1 == "")
            {
                Sql = "where [管材类别]=1 and dbo.材料.品名='成品套管接箍' and 管材到货登记.[车号] like '%" + temp + "%'";
            }
            else if (temp == "")
            {
                Sql = "where [管材类别]=1 and dbo.材料.品名='成品套管接箍' and [炉号] like '%" + temp1 + "%'";
            }
            else
            {
                Sql = "where [管材类别]=1 and dbo.材料.品名='成品套管接箍' and 管材到货登记.[车号] like '%" + temp + "%' and [炉号] like '%" + temp1 + "%'";
            }
            DateTime Dt;
            if (DateTime.TryParse(this.Date1.Text, out Dt))
            {
                Sql += " and [管材到货登记].[到货日期]>='" + Dt.ToShortDateString() + "'";
            }
            if (DateTime.TryParse(this.Date2.Text, out Dt))
            {
                Sql += " and [管材到货登记].[到货日期]<='" + Dt.ToShortDateString() + "'";
            }
            if (this.GongHuoDanWei.Text != "0")
            {
                Sql += " and [管材到货登记].[原料供货单位ID]=" + this.GongHuoDanWei.Text;
            }
            if (this.YuanCaiLiao.Text != "0")
            {
                Sql += " and [管材到货明细].[管材ID]=" + this.YuanCaiLiao.Text;
            }
            this.SqlDataSource1.SelectCommand = @"SELECT  dbo.管材到货登记.车号,dbo.原料供货单位.名称, dbo.材料.品名, dbo.管材到货明细.炉号, dbo.管材到货明细.批次, dbo.管材到货明细.重量,dbo.管材到货明细.磅重,dbo.管材到货明细.支数, dbo.管材到货明细.区域, 
                      dbo.管材到货登记.到货日期
                      FROM  dbo.管材到货登记 INNER JOIN
                      dbo.管材到货明细 ON dbo.管材到货登记.ID = dbo.管材到货明细.管材到货登记ID INNER JOIN
                      dbo.原料供货单位 ON dbo.管材到货登记.原料供货单位ID = dbo.原料供货单位.ID INNER JOIN
                      dbo.材料 ON dbo.管材到货明细.管材ID=dbo.材料.ID  " + Sql;
            string sql_temp = @"SELECT 车数=(select count(distinct dbo.管材到货登记.车号) from dbo.管材到货登记 INNER JOIN
                      dbo.管材到货明细 ON dbo.管材到货登记.ID = dbo.管材到货明细.管材到货登记ID INNER JOIN
                      dbo.原料供货单位 ON dbo.管材到货登记.原料供货单位ID = dbo.原料供货单位.ID INNER JOIN
                      dbo.材料 ON dbo.管材到货明细.管材ID=dbo.材料.ID  " + Sql + "), isnull(SUM(管材到货明细.重量),0),isnull(SUM(磅重),0) FROM  dbo.管材到货登记 INNER JOIN dbo.管材到货明细 ON dbo.管材到货登记.ID = dbo.管材到货明细.管材到货登记ID INNER JOIN dbo.原料供货单位 ON dbo.管材到货登记.原料供货单位ID = dbo.原料供货单位.ID INNER JOIN  dbo.材料 ON dbo.管材到货明细.管材ID = dbo.材料.ID " + Sql;
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            Cmd.CommandText = sql_temp;
            Cmd.CommandType = CommandType.Text;

            SqlDataReader Reader = Cmd.ExecuteReader();

            if (Reader.Read())
            {
                this.Label_Result.Text = "";
                this.Label_Result.Text = "车数：" + (int)Reader[0] + "车　　     名义重量合计：" + (double)Reader[1] + "吨　　     实际磅重合计：" + (double)Reader[2] + "吨<br>";
            }
            Reader.Close();
            Reader.Dispose();
            Cmd.Dispose();
            Conn.Close();
            Conn.Dispose();
            this.ViewState["SelectCommand"] = this.SqlDataSource1.SelectCommand;
        }

        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }
        }
    }
}
